package com.quanyan.travel.component.pushMsg;

import com.alibaba.fastjson.JSON;
import com.quanyan.common.http.HttpClientUtils;
import com.quanyan.messagepush.entity.*;
import com.quanyan.messagepush.enums.BoxName;
import com.quanyan.messagepush.enums.BoxType;
import com.quanyan.messagepush.enums.MsgType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Created by xiaoxiao on 16/7/14.
 */
@Component("travelMsgPush")
public class MsgPush {

    @Value("${message.push.service.url}")
    private String messagePushServiceUrl;

    private final static Logger logger = LoggerFactory.getLogger(MsgPush.class);

    private final static String DEFAULT_VAR = "变量";


    /**
     * 消息的key定义, 与数据库一致
     *
     * */

    public static final String USER_FAVORITE_ACTIVITY_PUBLISH_MSG = "USER_FAVORITE_ACTIVITY_PUBLISH_MSG";  //用户喜欢的活动发布
    public static final String USER_ACTIVITY_WAIT_PAY = "USER_ACTIVITY_WAIT_PAY";  //用户活动支付督促
    public static final String USER_PAY_SUCCESS_MSG = "USER_PAY_SUCCESS_MSG";  //用户支付成功
    public static final String USER_SUBMIT_REFUND_MSG = "USER_SUBMIT_REFUND_MSG";  //用户提交申请退款
    public static final String USER_REFUND_SUCCESS_MSG = "USER_REFUND_SUCCESS_MSG";  //用户退款成功
    public static final String EXCEPTION_ORDER_MSG = "EXCEPTION_ORDER_MSG";  //用户异常订单



    /**
     *  简单封装消息推送
     *
     * */
    @Async
    public void pushMessageOperate(TravelMsgModel stadiumMsgModel) {

        ReqPushMessage reqPushMessage = new ReqPushMessage();
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTag(BoxName.RACE.value()+"_" + System.currentTimeMillis());

        //定义消息的标题和body
        Alert alert = new Alert();
        alert.setTitle(stadiumMsgModel.getAlertTitle());
        alert.setBody(stadiumMsgModel.getAlertBody());

        //消息盒子
        Payload payload = new Payload();
        payload.setTitle(stadiumMsgModel.getMsgTitle());
        payload.setBoxType(BoxType.ACTIVITY_MSG.value());
        payload.setMsgType(stadiumMsgModel.getMsgType().value());
        payload.setUrl(stadiumMsgModel.getToUrl());
        payload.setTimestamp(System.currentTimeMillis());
        payload.setBody(stadiumMsgModel.getMsgBody());

        if (stadiumMsgModel.getMsgType().value() == MsgType.JUMP_MSG.value()) {
            //跳转的页面, params有效 id已经失效
            Jump jump = new Jump();
            jump.setIdType(stadiumMsgModel.getIdType().value());
            jump.setId(stadiumMsgModel.getToId());
            jump.setParam(stadiumMsgModel.getParams());
            payload.setJump(jump);
        }

        //消息封装
        pushMessage.setAlert(alert);
        pushMessage.setTo(stadiumMsgModel.getToUidList());
        pushMessage.setLogo("");
        pushMessage.setLogoUrl("");
        pushMessage.setPayload(payload);
        reqPushMessage.setMsgList(Arrays.asList(pushMessage));
        try {
            String postJson = new String(JSON.toJSONString(reqPushMessage).getBytes(), "UTF-8");
            String result = HttpClientUtils.postJson(messagePushServiceUrl, postJson);
            logger.info("户外消息推送,内容:{},返回状态:{}", postJson, result);
        } catch (Exception e) {
            logger.error("户外推送消息失败，原因:{}", e);
        }
    }

    /**
     * 替换模板里的变量.替换  你好,变量 为 你好,潇潇,注意必须为中文,字符
     *
     * */
    public static String msgUtils(String body, String ...params) {
        if (params!=null && params.length > 0) {
            for (String str: params) {
                body = body.replaceFirst(DEFAULT_VAR, str);
            }
        }
        return body;
    }
}

